package com.woniuxy.caradmin.service.impl;

import com.woniuxy.caradmin.entity.pojo.Insurerecord;
import com.woniuxy.caradmin.entity.pojo.Maintainrecord;
import com.woniuxy.caradmin.entity.pojo.Remind;
import com.woniuxy.caradmin.entity.pojo.Yearcheckrecord;
import com.woniuxy.caradmin.mapper.InsurerecordMapper;
import com.woniuxy.caradmin.mapper.MaintainrecordMapper;
import com.woniuxy.caradmin.mapper.RemindMapper;
import com.woniuxy.caradmin.mapper.YearcheckrecordMapper;
import com.woniuxy.caradmin.service.RemindService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.util.List;
@Service
public class RemindServiceImpl implements RemindService {
    @Autowired
    private RemindMapper remindMapper;

    @Override
    public boolean selectExpiretime() {
        //年检到期查询并添加至提醒表
        List<Yearcheckrecord> yearcheckrecordList = remindMapper.selectYearcheckrecordExpietime();
        if (yearcheckrecordList != null && yearcheckrecordList.size() > 0) {
            for (Yearcheckrecord yearcheckrecord : yearcheckrecordList) {
                Remind remindYearcheck = new Remind();
                    long longnow = System.currentTimeMillis();
                    Date yearcheckNexttime = yearcheckrecord.getNexttime();
                    long longyeartime = yearcheckNexttime.getTime();
                    //如果年检到期时间-系统当前时间的差值小于7天
                    if (longyeartime - longnow <= 7 * 24 * 3600 * 1000L) {
                        remindYearcheck.setVehiclenumid(yearcheckrecord.getVehiclefile().getId());
                        remindYearcheck.setExpiretime(yearcheckNexttime);
                        remindYearcheck.setTypee("年检到期提醒");
                        //查询距离到期时间小于7天的年检记录在提醒表中是否存在
                        Remind remind = remindMapper.selectRemind(remindYearcheck);
                        //不存在时增加提醒记录
                        if(remind==null){
                            remindMapper.addRemind(remindYearcheck);
                        }
                }

            }
        }
        //保养到期查询并添加至提醒表
        List<Maintainrecord> maintainrecordList = remindMapper.selectMaintainrecordExpietime();
        if (maintainrecordList != null&&maintainrecordList.size()>0) {
            for (Maintainrecord maintainrecord : maintainrecordList) {
                Remind remindMaintain = new Remind();
                long longnow = System.currentTimeMillis();
                long maintainNexttime = maintainrecord.getNexttime().getTime();
                if (maintainNexttime - longnow < 7 * 24 * 3600 * 1000L) {
                    remindMaintain.setVehiclenumid(maintainrecord.getVehiclefile().getId());
                    remindMaintain.setExpiretime(maintainrecord.getNexttime());
                    remindMaintain.setTypee("保养到期提醒");
                    Remind remind = remindMapper.selectRemind(remindMaintain);
                    if(remind==null){
                        remindMapper.addRemind(remindMaintain);
                    }
                }
            }
        }
        //保险到期查询并添加至提醒表
        List<Insurerecord> insurerecordList = remindMapper.selectInsurerecordExpietime();
        if (insurerecordList != null&&insurerecordList.size()>0) {
            for (Insurerecord insurerecord : insurerecordList) {
                Remind remindInsure=new Remind();
                long longnow=System.currentTimeMillis();
                long insureEndtime = insurerecord.getEndtime().getTime();
                if(insureEndtime-longnow<=7 * 24 * 3600 * 1000L){
                    remindInsure.setVehiclenumid(insurerecord.getVehiclefile().getId());
                    remindInsure.setExpiretime(insurerecord.getEndtime());
                    remindInsure.setTypee("保险到期提醒");
                    Remind remind = remindMapper.selectRemind(remindInsure);
                    if(remind==null){
                        remindMapper.addRemind(remindInsure);
                    }
                }else{

                }
            }
        }
        return true;
    }

    @Override
    public List<Remind> selectAllRemind() {
        List<Remind> remindList = remindMapper.selectAllRemind();
        return remindList;
    }
}
